package com.chinaums.xm.wechat.commons;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.chinaums.xm.wechat.domain.wechat.message.ImageMessage;
import com.chinaums.xm.wechat.domain.wechat.message.Item;
import com.chinaums.xm.wechat.domain.wechat.message.NewsMessage;
import com.chinaums.xm.wechat.domain.wechat.message.TextMessage;
import com.thoughtworks.xstream.XStream;

/**
 * 微信消息处理
 * @author chenjf
 *
 */
public class MessageUtil {

	/**
	 * 将请求xml转成map
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
		final Map<String, String> map = new HashMap<>();
		final SAXReader reader = new SAXReader();
		final InputStream in = request.getInputStream();
		final Document document = reader.read(in);
		final Element root = document.getRootElement();
		@SuppressWarnings("unchecked")
		final List<Element> elements = root.elements();
		for (Element element : elements) {
			map.put(element.getName(), element.getTextTrim());
		}
		if (in != null) {
			in.close();
		}
		return map;
	}

	/**
	 * 将对象转为xml
	 * 
	 * @param textMessage
	 * @return
	 */
	public static String textMessageToXml(TextMessage textMessage) {
		XStream stream = new XStream();
		stream.alias("xml", textMessage.getClass());
		return stream.toXML(textMessage);
	}
	
	/**
	 * 将对象转为xml
	 * 
	 * @param imageMessage
	 * @return
	 */
	public static String imageMessageToXml(ImageMessage imageMessage) {
		XStream stream = new XStream();
		stream.alias("xml", imageMessage.getClass());
		return stream.toXML(imageMessage);
	}
	
	/**
	 * 将对象转为xml
	 * 
	 * @param newsMessage
	 * @return
	 */
	public static String newsMessageToXml(NewsMessage newsMessage) {
		final XStream stream = new XStream();
		stream.alias("xml", newsMessage.getClass());
		stream.alias("item", new Item().getClass());
		return stream.toXML(newsMessage);
	}
	
	public static String menuList(){
		final StringBuffer sb = new StringBuffer();
		sb.append("欢迎您的关注，请按菜单提示进行操作：\r\n");
		sb.append("输入手机号查询手机号归属信息\n");
		sb.append("输入身份证号查询身份证归属信息\n");
		sb.append("回复\"?\"调出次菜单");
		return sb.toString();
	}
	
	public static String first(){
		final StringBuffer sb = new StringBuffer();
		sb.append("该公众号供学习使用");
		return sb.toString();
	}
	
	public static String second(){
		final StringBuffer sb = new StringBuffer();
		sb.append("作者:陈俊飞\n");
		sb.append("职业:java软件工程师\n");
		sb.append("联系方式:18050012583\n");
		return sb.toString();
	}
	
	public static String unsubscribe(){
		final StringBuffer sb = new StringBuffer();
		sb.append("感谢您的使用!");
		return sb.toString();
	}
	
	public static String invalid(){
		final StringBuffer sb = new StringBuffer();
		sb.append("无效操作，请按菜单提示操作，谢谢!");
		return sb.toString();
	}
}
